//
//  API_Multithreading_SafeArray_Demo2VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/7/25.
//

import UIKit

class API_Multithreading_SafeArray_Demo2VC: UIViewController {

    private var array: DQThreadSafeArray<Int> = DQThreadSafeArray()
//    private var array: PRThreadSafeArray<Int> = PRThreadSafeArray()
//    private var array: [Int] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        test1()
        test2()
        test3()
    }
    
    private func test1() {
        for i in 1...20 {
            array.append(i)
            print("Thread is \(Thread.current) \(#function), i is \(i), count is \(array.count)")
            self.array.forEach({print("11 \($0)")})
        }
    }

    private func test2() {
        DispatchQueue.global().async {
            for i in 1...20 {
                self.array.append(i+50)
                print("Thread is \(Thread.current) \(#function), i is \(i), count is \(self.array.count)")
                self.array.forEach({print("22 \($0)")})
            }
        }
    }
    
    private func test3() {
        let queue = DispatchQueue(label: "com.jianbo.pei.PRThreadSafeArray", attributes: .concurrent)
        queue.async {
            for i in 1...20 {
                self.array.append(i+100)
                print("Thread is \(Thread.current) \(#function), i is \(i), count is \(self.array.count)")
                self.array.forEach({print("33 \($0)")})

            }
        }
    }
}
